package cn.lutu.adapter.controller;

import cn.lutu.dao.mapper.CommentMapper;
import cn.lutu.domain.service.CommentService;
import cn.lutu.protocol.dto.CommentPostDTO;
import cn.lutu.protocol.vo.CommentVO;
import cn.lutu.security.jwtFilter.CurrentPrincipal;
import cn.lutu.util.response.ResultVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Slf4j
@Api(tags = "评论模块")
@RestController
@RequestMapping("/v1/comments/")
public class CommentController {

    @Autowired
    CommentMapper mapper;
    @Autowired
    CommentService commentService;

    @ApiOperation("发表评论")
    @ApiOperationSupport(order = 1)
    @PostMapping("post")
    public ResultVO post(CommentPostDTO commentPostDTO,
                         @AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal){
        log.debug("开始处理发表评论的请求,参数{}",commentPostDTO);
        log.debug("currentPrinciple为{}",currentPrincipal);
        commentService.insert(commentPostDTO,currentPrincipal);
        return ResultVO.ok();
    }

    @ApiOperation("获取评论列表")
    @ApiOperationSupport(order = 2)
    @GetMapping("list/{id}")
    public ResultVO selectById(@PathVariable Long id){
        List<CommentVO> list = commentService.selectById(id);
        log.info("评论查询结果为{},",list);
        return ResultVO.ok(list);
    }

    @ApiOperation("删除评论")
    @ApiOperationSupport(order = 3)
    @PostMapping("/delete")
    public ResultVO deleteById(Long id){
        commentService.deleteById(id);
        return ResultVO.ok();
    }


}
